package Leetcode第二期;

import 多线程.S;

/**
 * @author : K k
 * @date : 21:02 2020/9/17
 */
public class 压缩字符串_443 {

    public int compress(char[] chars) {
        int p=0,q=0;//相同类型字符的起止指针

        for (int i=0;i<chars.length;i++){
            if (i+1==chars.length || chars[i+1]!=chars[i]){
                chars[p++]=chars[q];
                if (i>q){
                    int len=i-q+1;
                    for(char c:(String.valueOf(len)).toCharArray()){
                        chars[p++]=c;
                    }
                }
                q=i+1;
            }
        }
        System.out.println(chars);
        return p;

    }

    public int compressA(char[] chars) {
        StringBuilder help=new StringBuilder();
        help.append(chars[0]);
        int length=1;

        for (int i=1;i<chars.length;i++){
            if (chars[i]==chars[i-1]){ length++;}
            else if (length>1){
                help.append(length);
                help.append(chars[i]);
                length=1;
            }
        }
        help.append(length);
        chars=help.toString().toCharArray();
        System.out.println(help);
        return help.length();
    }

    public static void main(String[] args) {
        压缩字符串_443 x=new 压缩字符串_443();
        x.compress(new char[]{'a','b','b','c','c','c'});
    }
}
